//
//  WBWelcomeView.swift
//  我的微博
//
//  Created by 袁 学虎 on 2017/2/25.
//  Copyright © 2017年 袁 学虎. All rights reserved.
//

import UIKit
import SDWebImage

/// 欢迎使用
class WBWelcomeView: UIView {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var topCons: NSLayoutConstraint!
    
    
    
    class func welcomeView() -> WBWelcomeView {
        
        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        
        // 从 XIB 加载的视图，默认是 600 * 600
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    
    override func awakeFromNib() {
        
        // 1. url
        guard let urlString = WBNetworkManager.shared.userAccount.avatar_large,
              let url = URL(string: urlString) else {
                
                return
        }
        
        // 2. 设置头像
        // 如果不绑定占位图像，之前设置的图像会被清空
        iconView.sd_setImage(with: url,
                             placeholderImage: UIImage(named: "avatar_default_big"))
        
        // 3. FIXME: - 设置圆角
        iconView.layer.cornerRadius = iconView.bounds.width * 0.5
        iconView.layer.masksToBounds = true
        
        
    }

    /// 视图被添加到 window 上，表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        self.layoutIfNeeded()
        
        topCons.constant = bounds.size.height - 500
        
        UIView.animate(withDuration: 2.0,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: { 
                        
                        // 更新约束
                        self.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1.0,
                           animations: {
                
                self.tipLabel.alpha = 1
            }, completion: { (_) in
            
                self.removeFromSuperview()
            })
        }
    }
    

}


